package Tienda;

import Librerias.Productos;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "Sumacarro", urlPatterns = {"/Sumacarro"})
public class Sumacarro extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            boolean eliminar = Boolean.parseBoolean(request.getParameter("eliminar"));
            boolean actualizar = Boolean.parseBoolean(request.getParameter("act"));
            int id=Integer.parseInt(request.getParameter("id"));
            HttpSession sesion=request.getSession(true); 
            List<Productos> lista = new ArrayList<Productos>();
            if(sesion.getAttribute("lista")!=null)
                    lista=(List<Productos>)sesion.getAttribute("lista");
            
            
            if(eliminar){
                comprobar(id,lista,true);
                
            }
            else if(actualizar){
                int cant = Integer.parseInt(request.getParameter("val"));
                comprobar(id,lista,true);
                insertar(id, lista, sesion, cant);
            }else{
                if (comprobar(id,lista,false)){
                    insertar(id, lista, sesion, 1);
                } 
            }
        } finally {
            out.println("<META HTTP-EQUIV='REFRESH' CONTENT='0;URL=Carrito.jsp'>");
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    public boolean comprobar(int id, List<Productos> lista, boolean eliminar){
        boolean bandera=true;
        Iterator<Productos> iter = lista.iterator();
        while (iter.hasNext() && bandera){
        Productos producto=iter.next();
            if (producto.getId() == id){
                bandera = false;
                if(eliminar){
                    lista.remove(producto);
                }
            }
        }
        return bandera;
    }
    
    public void insertar(int id, List<Productos> lista, HttpSession sesion, int cantidad){
        Productos prod = new Productos();
        prod.setId(id);
        prod.setCantidad(cantidad);
        lista.add(prod);
        sesion.setAttribute("lista",lista);
    }
}
